package kines.ct;

/**
 * <p>Title: CT</p>
 * <p>Description: Intranet Consorcio Terrenos</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Consorcio Terrenos</p>
 * @author Jose Delgado
 * @version 1.0
 */

import java.text.NumberFormat;
import java.util.Locale;

public class NumFormat{
  private static String uniti[] = {
      "un", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve"};
  private static String deci[] = {
      "diez", "veinte", "treinta", "cuarenta", "cincuenta", "sesenta",
      "setenta", "ochenta", "noventa"};
  private static String cent[] = {
      "cien", "docientos", "trecientos", "cuatrocientos", "quinientos",
      "seisientos", "setecientos", "ochocientos", "novecientos"};
  private static String rest[] = {
      "once", "doce", "trece", "catorce", "quince"};
  /**
   * Convert number to letters each 3 digits
   * @param ent
   * @return
   */
  private static String convert(String ent) throws NumberFormatException{
    String retVal = "";
    String otca = "";
    int cente, dec, unit;
    int len = ent.length();
    unit = Integer.parseInt( ent.substring(0,1) );
    if( len > 1 ){
      dec = Integer.parseInt( ent.substring(1,2) );
      if( len > 2 ){
        cente = Integer.parseInt( ent.substring(2,3) );
      }else{
        cente = 0;
      }
    }else{
      dec = 0;
      cente = 0;
    }
    if( cente > 0 ){
      if( cente == 1 ){
        if( dec == 0 ){
          if( unit == 0 ){
            otca = cent[cente-1];
          }else{
            otca = cent[cente-1]+"to ";
          }
        }else{
          otca = cent[cente-1]+"to ";
        }
      }else{
        otca = cent[cente-1]+"";
      }
    }
    if( dec > 0 ){
      if( dec == 1 ){
        if( unit == 0 ){
          otca += deci[dec-1];
        }else{
          if( !( unit >= 1 && unit <= 5 ) ){
            otca += deci[dec-1] + " y ";
          }
        }
      }else{
        if( dec == 2 ){
          if( unit == 0 ){
            otca += deci[dec-1];
          }else{
            otca += "veinti";
          }
        }else{
          otca += deci[dec-1]+(unit==0?" ":" y ");
        }
      }
    }
    if( unit > 0 ){
      if( dec == 0 ){
        otca += uniti[unit-1];
      }else{
        if( dec == 1 ){
          if( unit >=1&&unit<=5){
            otca += rest[unit-1];
          }else{
            otca += uniti[unit-1];
          }
        }else{
          otca += uniti[unit-1];
        }
      }
    }
    return otca;
  }
  /**
   * Convert Number to Letters like 120,000 to Ciento veinte mil pesos
   * @param number Double to convert
   * @return String with description of number
   * @throws java.lang.NumberFormatException
   */
  public static String formatLetters( double number )throws NumberFormatException{
    String str_number_return = "";
    NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
    nf.setMinimumFractionDigits(2);
    nf.setMaximumFractionDigits(2);
    StringBuffer str_number = new StringBuffer( nf.format(number) );
    String str_number_cad = "";
    String millions = "";
    String decen = "";
    String centen = "";
    int len = str_number.length();
    if( len > 12 )return null;
    else{
      if( number == 0 ){
        str_number_return = "cero";
      }else{
        str_number_cad = str_number.reverse().toString();
        if( len > 3 ){
          centen = str_number_cad.substring( 3, str_number_cad.length()<6?str_number_cad.length():6 );
          if( len > 7 ){
            decen = str_number_cad.substring( 7, str_number_cad.length()<10?str_number_cad.length():10 );
            if( len > 11 ){
              millions = str_number_cad.substring( 11, str_number_cad.length()<14?str_number_cad.length():14 );
              str_number_return = convert( millions ) + (Integer.parseInt(millions)==1?" millon ":" millones ");
            }
            str_number_return += convert( decen ) + " mil ";
          }
          str_number_return += convert( centen );
          str_number_return += " Pesos "+( new StringBuffer( str_number_cad.substring( 0, 2 ) ).reverse() );
        }else{
          return null;
        }
      }
    }
    return str_number_return;
  }
}
